package com.quyunshuo.module.home.util

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.PermissionUtils

object PermissionHelper {
    @RequiresApi(Build.VERSION_CODES.Q)
    const val  ACTIVITY_RECOGNITION = Manifest.permission.ACTIVITY_RECOGNITION

     fun requestActivityRecognitionPermission(activity: FragmentActivity, onGranted:()->Unit, onDenied:()-> Unit,onReject:()-> Unit = {}) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            PermissionUtils.permission(ACTIVITY_RECOGNITION)
                .callback(object : PermissionUtils.SimpleCallback {
                    override fun onGranted() {
                        onGranted.invoke()
                    }

                    override fun onDenied() {
                        onDenied.invoke()
                        if (ContextCompat.checkSelfPermission(
                                activity,
                                Manifest.permission.ACTIVITY_RECOGNITION
                            ) == PackageManager.PERMISSION_DENIED
                            && !ActivityCompat.shouldShowRequestPermissionRationale(
                                activity,
                                Manifest.permission.ACTIVITY_RECOGNITION
                            )
                        ) {
                            onReject.invoke()
                        }

                    }
                }).request()
        }else{
            onGranted.invoke()
        }
    }
}